package com.nopay.cdxphone.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.nopay.cdxphone.R;

public class DialogDelayForAutoCall extends Dialog {

    private Activity mActivity;

    public DialogDelayForAutoCall(@NonNull Activity context) {
        super(context, R.style.MyDialog);
        mActivity = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_delay_for_auto_call);

        setCancelable(false);
        findViewById(R.id.tv_stop_auto_call).setOnClickListener(v -> {
            if(mCountDownTimer != null) {
                mCountDownTimer.cancel();
                mCountDownTimer = null;
            }
            if(onStopCallListener != null) onStopCallListener.onStopCall();

            dismiss();
        });
    }

    private CountDownTimer mCountDownTimer;

    public void show(int second) {
        super.show();
        TextView tv_time_count_down =  findViewById(R.id.tv_time_count_down);
        tv_time_count_down.setText(second + "秒后拨打");

        mCountDownTimer = new CountDownTimer(second * 1000,  1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tv_time_count_down.setText((millisUntilFinished / 1000) + "秒后拨打");
            }

            @Override
            public void onFinish() {
                mCountDownTimer = null;
                if(onDelayCompleteListener != null) onDelayCompleteListener.onDelayComplete();

                dismiss();
            }
        };
        mCountDownTimer.start();
    }

    private OnDelayCompleteListener onDelayCompleteListener;

    public DialogDelayForAutoCall setOnDelayCompleteListener(OnDelayCompleteListener onDelayCompleteListener) {
        this.onDelayCompleteListener = onDelayCompleteListener;
        return this;
    }

    public interface OnDelayCompleteListener {

        void onDelayComplete();
    }

    private OnStopCallListener onStopCallListener;

    public DialogDelayForAutoCall setOnStopCallListener(OnStopCallListener onStopCallListener) {
        this.onStopCallListener = onStopCallListener;
        return this;
    }

    public interface OnStopCallListener {

        void onStopCall();
    }
}
